<?php

namespace App\Models;



class Notification extends Base
{
    const TYPE_SYSTEM = 5;
    const TYPE_FOCUS = 2;
    const TYPE_ARTICLE_STAR = 1;
    const TYPE_COMMENT_STAR = 7;
    const TYPE_POST_COMMENT = 3;
    const TYPE_POST_REPLY = 4;
    const TYPE_BACKEND_CREATE = 8;

    protected $appends = ['format_time'];

    public function user() {
        return $this->belongsTo(User::class, 'user_id');
    }

    public function comment() {
        return $this->belongsTo(Comment::class, 'comment_id');
    }

    public function entity() {
        return $this->belongsTo(Article::class, 'entity_id');
    }

    public function getFormatTimeAttribute() {
        $second = time() - strtotime($this->created_at);
        $arr = time2string($second);
        $day = $arr['day'];
        if ($day >= 10)
            return date('Y-m-d', strtotime($this->created_at));
        else if($day >=1 && $day < 10) {
            return $day . '天前';
        } else {
            $hour = $arr['hour'];
            if ($hour >= 1) {
                return $hour . '小时前';
            } else {
                $minute = $arr['minute'];
                if ($minute <= 1)
                    return '刚刚';
                else
                    return $minute . '分钟前';
            }
        }

    }

}
